package com.lanou3g.hw0520.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {
    private static String url;
    private static String driverclass;
    private static Properties info = new Properties();

    static {
        InputStream fis= JDBCUtil.class.getClassLoader()
                .getResourceAsStream("JDBCProp.properties");
        try {
            info.load(fis);
            url= info.getProperty("url");
            driverclass= info.getProperty("driverclass");
            Class.forName(driverclass);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            IOUtil.closeAll(fis);
        }
    }
    public static Connection getConn(){
        Connection conn= null;
        try {
            conn = DriverManager.getConnection(url,info);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static MyStatement statement(String sql){
        Connection conn=getConn();
        PreparedStatement ps=null;
        try {
             ps= conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new MyStatement(ps);


    }



    public static void closeResource(Statement statement) {
        try {
            // 可以从statement中获取连接对象
            Connection conn = statement.getConnection();
            // 关闭资源
            IOUtil.closeAll(statement, conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
